import { getSysConfigPageList } from "@/api/sys/sysConfig";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { RootState } from "..";
import { global_const } from "@/utils/constent";

const initialState = {
  sysConfigPageLoading: false,
  sysConfigPageList: {
    current: global_const.default_page_current,
    pages: 1,
    records: [],
    total: global_const.default_page_total,
    size: global_const.default_page_size,
  } as PageType<SysConfigType>,
  SysConfigPageParams: {
    current: global_const.default_page_current,
    size: global_const.default_page_size,
  },
  isShowConfigModal: {
    show: false,
    // 1 新增 2 修改
    mode: 1,
    title: "新增",
    data: {} as Partial<SysConfigType>,
  },
};

export const get_sysConfig_page_list = createAsyncThunk<
  PageType<SysConfigType>,
  SysConfigPageParams
>("get/sysConfig_page_list", async (action: any) => {
  return await getSysConfigPageList(action);
});

const sysConfigSlice = createSlice({
  name: "sysConfig",
  initialState,
  reducers: {
    set_is_show_config_modal(state, action) {
      state.isShowConfigModal = action.payload;
    },

    set_sys_config_page_params(state, action) {
      state.SysConfigPageParams = action.payload;
    },
  },
  extraReducers(builder) {
    builder
      .addCase(get_sysConfig_page_list.pending, (state, action) => {
        state.sysConfigPageLoading = true;
      })
      .addCase(get_sysConfig_page_list.fulfilled, (state, action) => {
        state.sysConfigPageList = action.payload;
        state.sysConfigPageLoading = false;
      });
  },
});

export const selectSysConfigPageList = (state: RootState) =>
  state.sysConfig.sysConfigPageList;

export const selectIsShowConfigModal = (state: RootState) =>
  state.sysConfig.isShowConfigModal;

export const selectSysConfigPageLoading = (state: RootState) =>
  state.sysConfig.sysConfigPageLoading;

export const selectSysConfigPageParams = (state: RootState) =>
  state.sysConfig.SysConfigPageParams;

export const { set_is_show_config_modal, set_sys_config_page_params } =
  sysConfigSlice.actions;
export default sysConfigSlice.reducer;
